//
//  DetailTopView.m
//  FKGW
//
//  Created by song ce on 2017/5/4.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "DetailEvaluateView.h"
#import "LayerCell.h"
#import "GoodsCellTableViewCell.h"
#import "GoodsLastButtonCell.h"
#import "AllEvaluateHeadV.h"
#import "UIView+Extend.h"
@interface DetailEvaluateView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;

@property(nonatomic,strong)AllEvaluateHeadV *allHeadView;
@property(nonatomic,assign)BOOL isHaveEvaluate;
@end
@implementation DetailEvaluateView
-(void)dealloc{
    [[NSNotificationCenter defaultCenter ]removeObserver:self];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.dataSource = [NSMutableArray array];
        [self _initTopView];
        [self tableViewAndHeaderView];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(evaluateResults:) name:@"evaluateResults" object:nil];
    }
    return self;
}

-(void)evaluateResults:(NSNotification*)noti{
    NSDictionary *dataDic = noti.object;
    //评论列表
    NSArray *listArr = dataDic[@"contents"];
    if(self.dataSource.count){
        [self.dataSource removeAllObjects];
    }
    for(NSDictionary *dic in listArr){
        EvaluateListModel *model = [EvaluateListModel  modelWithDic:dic];
        [self.dataSource addObject:model];
    }
    if(self.dataSource.count){
        self.isHaveEvaluate = YES;
        self.tableV.height = 0+40+55*self.dataSource.count;
    }else{
        self.isHaveEvaluate = NO;
        self.tableV.height = 0+55*self.dataSource.count;
    }
//    [self.dataSource addObject:@""];
//    [self.dataSource addObject:@""];
//    [self.dataSource addObject:@""];
//    [self.dataSource addObject:@""];

    [self.tableV reloadData];
    //设置详情页面评论显示数量
    self.allHeadView.evaluateCountLabel.text = [NSString stringWithFormat:@"商品评价  （%ld）", self.dataSource.count];
    self.height = self.allHeadView.height +self.tableV.height;
}
-(void)_initTopView{
    //top视图（评价按钮）
    _allHeadView =   [[[NSBundle mainBundle]loadNibNamed:@"AllEvaluateHeadV" owner:self options:nil]lastObject];
    _allHeadView.top =0;
    _allHeadView.left = 0;
    [self addSubview:_allHeadView];
    
  //  根据选择的评论按钮加载不同的数据
    __weak typeof(self) weakSelf = self;
    _allHeadView.evaluateBlock =^(int count){
        if(weakSelf.evaluateBlock){
            weakSelf.evaluateBlock(count);
        }
    };
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, self.allHeadView.bottom, Main_Screen_Width, 40+55*2) style:UITableViewStylePlain];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    [self addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.bounces =NO;
    _tableV.scrollEnabled = NO;
    self.height = self.allHeadView.height+self.tableV.height;
}

-(void)setPid:(NSString *)pid{
    _pid = pid;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.isHaveEvaluate){
//         return self.dataSource.count+2;
        return self.dataSource.count+1;
    }
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.isHaveEvaluate){
        if(indexPath.row==self.dataSource.count){
            GoodsLastButtonCell    *cell=[[[NSBundle mainBundle]loadNibNamed:@"GoodsLastButtonCell" owner:self options:nil]lastObject];
            cell.pid = self.pid;
            return cell;
        }
//        if(indexPath.row==self.dataSource.count+1){
//            LayerCell    *cell=[[[NSBundle mainBundle]loadNibNamed:@"LayerCell" owner:self options:nil]lastObject];
//            return cell;
//        }
    }else{
        if(indexPath.row==self.dataSource.count){
            LayerCell    *cell=[[[NSBundle mainBundle]loadNibNamed:@"LayerCell" owner:self options:nil]lastObject];
            return cell;
        }
    }
    
    NSString *ident=@"actell";
    GoodsCellTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"GoodsCellTableViewCell" owner:self options:nil]lastObject];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.isHaveEvaluate){
        if(indexPath.row==self.dataSource.count){
            return 40;
        }
//        if(indexPath.row==self.dataSource.count+1){
//            return 147;
//        }
    }else{
//        if(indexPath.row==self.dataSource.count){
//            return 147;
//        }
    }
    return 55;
}


@end
